﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using OgilvyOne.BLL;
using System.Data;
using OgilvyOne.Borad.DAL;
using System.IO;


public partial class admin_vico_preorder_list : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params["layerid"]);

        }
        GridView1DataBind();
    }

    private void GridView1DataBind()
    {
        int pagesize = 15;
        if (Request.Params["pageNum"] != null && Request.Params["pageNum"] != "")
        {
            try
            {
                GridView1.PageIndex = int.Parse(Request.Params["pageNum"]) - 1;
            }
            catch { }
        }

        DataSet ds = GetReg();

        GridView1.DataSource = ds;
        GridView1.DataBind();

        this.pagehtml.Text = getPageHtml(GridView1.PageCount, GridView1.PageIndex + 1, ds.Tables[0].Rows.Count);
    }

    private DataSet GetReg()
    {
        string strSql = "select id,name,sex,mobile,email,province,city,dealer,budget,mention,capcity,carModel,carColor,carSeat,creatTime,source,channel from preorder order by creatTime desc ";
        return SQLHelper.Query(strSql);
    }

    //导出方法
    private void output(DataSet ds)
    {

        if (ds.Tables[0].Rows.Count != 0)
        {
            DataTable dt = ds.Tables[0];
            StringWriter sw = new StringWriter();
            //sw.WriteLine(Session["msg"].ToString() + "\t" );
            sw.WriteLine("ID,姓名,称谓,电话号码,邮箱,省,市,经销商,购车预算,购车时间,排量,车型,颜色,坐垫,注册时间");
            foreach (DataRow dr in dt.Rows)
            {
                sw.WriteLine(dr["id"] + "," + dr["name"] + "," + dr["sex"] + "," + dr["mobile"] + "," + dr["email"] + "," + dr["province"] + "," + dr["city"] + "," + dr["dealer"] + "," + dr["budget"] + "," + dr["mention"] + "," + dr["capcity"] + "," + dr["carModel"] + "," + dr["carColor"] + "," + dr["carSeat"] + "," + dr["creatTime"] + "," + dr["source"] );
            }
            sw.Close();
            string sss = DateTime.Today.ToString("yyyyMMdd") + new Random(DateTime.Now.Millisecond).Next(10000).ToString() + ".csv";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + sss);
            Response.ContentType = "application/ms-excel";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.Write(sw);
            Response.End();
        }
    }

    //导出按钮
    protected void Add_click(object sender, EventArgs e)
    {
        output(GetReg());
    }
    protected void Mod_click(object sender, EventArgs e)
    {
        Response.Redirect("edmreg_detail.aspx?id=" + ((LinkButton)sender).CommandArgument + "&layerid=" + Request.Params["layerid"], true);
    }

    protected void Del_click(object sender, EventArgs e)
    {
        ////this.PopedomValidate(Request.Params["layerid"], DEL);
        //if (new VicoDealerBll().Del(int.Parse(((LinkButton)sender).CommandArgument)))
        //{
        //    DataBind();
        //}
    }
}